package com.wjc.train.member.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wjc.train.common.req.MemberTicketSaveReq;
import com.wjc.train.common.untils.CopyUtil;
import com.wjc.train.member.entity.Ticket;
import com.wjc.train.member.repository.TicketRepository;
import com.wjc.train.member.req.TicketQueryReq;
import com.wjc.train.member.req.TicketSaveReq;
import com.wjc.train.member.resp.TicketQueryResp;
import com.wjc.train.member.service.ITicketService;
import io.seata.core.context.RootContext;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 会员车票 服务实现类
 * </p>
 *
 * @author Guessed
 * @since 2023-08-22
 */
@Service
@Slf4j
public class TicketServiceImpl  implements ITicketService {

    @Resource
    private TicketRepository ticketRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(TicketSaveReq ticketSaveReq) {
        Ticket ticket = CopyUtil.copy(ticketSaveReq, Ticket.class);
        ticketRepository.saveOrUpdate(ticket);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(MemberTicketSaveReq ticketSaveReq) {
        Ticket ticket = CopyUtil.copy(ticketSaveReq, Ticket.class);
        ticketRepository.save(ticket);
    }

    @Override
    public IPage<TicketQueryResp> queryList(IPage<TicketQueryResp> page, TicketQueryReq ticketQueryReq) {
        return ticketRepository.queryList(page,ticketQueryReq);
    }

    @Override
    public void delete(Long id) {
        ticketRepository.getBaseMapper().deleteById(id);
    }

    @Override
    public void saveList(List<MemberTicketSaveReq> ticketSaveReqList) {
        log.info("seata全局事务id：{}", RootContext.getXID());
        ticketRepository.saveBatch(CopyUtil.copyList(ticketSaveReqList,Ticket.class));
    }
}
